package com.bowlong.third.redis.lang;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import redis.clients.jedis.Jedis;

public class JedisSet {
	final Jedis jedis;
	final String pkey;

	public JedisSet(Jedis jedis, String pkey) {
		this.jedis = jedis;
		this.pkey = pkey;
	}

	public List<String> getAll() {
		List<String> r2 = new Vector<>();
		Set<String> datas = this.jedis.smembers(pkey);
		for (String tex : datas) {
			r2.add(tex);
		}
		return r2;
	}

	public int size() {
		return this.jedis.scard(pkey).intValue();
	}

	public boolean isEmpty() {
		return size() <= 0;
	}

	public boolean contains(String tex) {
		return this.jedis.sismember(pkey, tex);
	}

	public Iterator<String> iterator() {
		List<String> datas = getAll();
		return datas.iterator();
	}

	public String[] toArray() {
		List<String> datas = getAll();
		String[] a = new String[datas.size()];
		return datas.toArray(a);
	}

	public String[] toArray(String[] a) {
		List<String> datas = getAll();
		return datas.toArray(a);
	}

	public boolean add(String tex) {
		this.jedis.sadd(pkey, tex);
		return true;
	}

	public boolean remove(String tex) {
		this.jedis.srem(pkey, tex);
		return true;
	}

	public boolean containsAll(Collection<String> c) {
		List<String> texs = new Vector<>();
		for (String tex : c) {
			texs.add(tex);
		}
		Set<String> datas = this.jedis.smembers(pkey);
		return datas.containsAll(texs);
	}

	public boolean addAll(Collection<String> c) {
		String[] a = new String[c.size()];
		c.toArray(a);
		this.jedis.sadd(pkey, a);
		// for (String tex : c) {
		// this.jedis.sadd(pkey, tex);
		// }
		return true;
	}

	public boolean retainAll(Collection<String> c) {
		List<String> texs = new Vector<>();
		for (String tex : c) {
			texs.add(tex);
		}
		Set<String> datas = this.jedis.smembers(pkey);
		return datas.retainAll(texs);
	}

	public boolean removeAll(Collection<String> c) {
		String[] a = new String[c.size()];
		c.toArray(a);
		this.jedis.srem(pkey, a);
		// for (String tex : c) {
		// this.jedis.srem(pkey, tex);
		// }
		return true;
	}

	public void clear() {
		this.jedis.del(pkey);
	}

}
